Imports System.Windows.Forms

Public Class frmOpenAlternate

    Friend Property SelectedTable As String = ""
    Private proj As clsProject = Nothing

    Private Sub frmStartup_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Text = String.Format("Welcome to WRDB {0}", My.Application.Info.Version.ToString(2))
            AddProjects()
            MainWRDB.CloseSplashScreen()
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub lstProjects_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstProjects.SelectedIndexChanged
        AddTables()
    End Sub

    Private Sub lstTables_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstTables.DoubleClick
        btnOpen.PerformClick()
    End Sub

    Private Sub lstTables_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstTables.SelectedIndexChanged
        btnOpen.Enabled = lstTables.SelectedIndex <> -1
    End Sub

    Private Sub AddProjects()
        If MainForm Is Nothing Then Exit Sub
        With lstProjects
            .BeginUpdate()
            .Items.Clear()
            For Each kv As Generic.KeyValuePair(Of String, String) In clsConnections.GetProjectList
                clsConnections.GetDatasource(kv.Key)
                If String.Equals(clsConnections.GetDatasource(kv.Key), Project.DB.DataSource, StringComparison.InvariantCultureIgnoreCase) AndAlso kv.Key <> Project.Name Then
                    .Items.Add(kv.Key)
                End If
            Next
            .EndUpdate()
            If .Items.Count > 0 Then lstProjects.SelectedIndex = 0
        End With
    End Sub

    Private Sub AddTables()
        Try
            With lstTables
                .BeginUpdate()
                .Items.Clear()
                proj = New clsProject(lstProjects.Text, False)
                For Each s As String In proj.GetTableList(clsProject.enumTableTypes.Master, clsProject.enumTableTypes.Working)
                    .Items.Add(s)
                Next
                .EndUpdate()
                If .Items.Count > 0 Then lstTables.SelectedIndex = 0
            End With
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        Try
            SelectedTable = $"{proj.DB.DatabaseName}.DBO.{lstTables.Text}"
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

End Class
